package com.example.show2page.service.impl;

import com.example.show2page.mapper.TableDataMapper;
import com.example.show2page.mapper.TableStructMapper;
import com.example.show2page.pojo.ConditionsVO;
import com.example.show2page.pojo.PageVO;
import com.example.show2page.pojo.TableData;
import com.example.show2page.pojo.TableStruct;
import com.example.show2page.service.PageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.awt.TimedWindowEvent;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author Hacah
 * @date 2021/4/23 10:27
 */
@Service
public class PageServiceImpl implements PageService {

    @Autowired
    private TableDataMapper tableDataMapper;

    @Autowired
    private TableStructMapper tableStructMapper;

    @Override
    public PageInfo<TableData> getTableData(PageVO pageVO) {


        PageHelper.startPage(pageVO.getPageNum(),pageVO.getPageSize());
        // 第一部分数据库里的数据

        // 条件查询
        Example example = new Example(TableData.class);
        // 查询内容
        String queryContent = "%" + pageVO.getQueryContent() + "%";
        // 创建条件查询
        Example.Criteria criteria = example.createCriteria();
        // 获取页面VO的查询条件
        List<String> queryCondition = pageVO.getQueryCondition();
        if (queryCondition != null && queryCondition.size() > 0) {
            for (String s : queryCondition) {
                criteria.orLike(s, queryContent);
            }
        }
        List<TableData> tableData = tableDataMapper.selectByExample(example);

        PageInfo<TableData> listPageInfo = new PageInfo<TableData>(tableData);
        return listPageInfo;
    }

    @Override
    public PageInfo<TableStruct> getTableStruct(String tableName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);

        Example example = new Example(TableStruct.class);
        example.setDistinct(true);
        example.createCriteria().andEqualTo("bywmc", tableName);
        List<TableStruct> tableStructs = tableStructMapper.selectByExample(example);

        PageInfo<TableStruct> tableStructPageInfo = new PageInfo<>(tableStructs);
        return tableStructPageInfo;
    }

    @Override
    public ConditionsVO getSysCondition() {
        ConditionsVO conditionsVO = new ConditionsVO();
        conditionsVO.setSysName(tableDataMapper.selectAllDiffSysName());
        return conditionsVO;
    }

    @Override
    public ConditionsVO getOtherCondition(String sysName, String level1, String level2, String level3, String level4) {
        ConditionsVO conditionsVO = new ConditionsVO();
        if (sysName != null && level1 != null && level2 != null && level3 != null) {
            // 返回第四级条件
            conditionsVO.setLevel4(tableDataMapper.selectAllDiffLeve4(sysName, level1, level2, level3));
            return conditionsVO;
        } else if (sysName != null && level1 != null && level2 != null) {
            // 返回第三级条件
            conditionsVO.setLevel3(tableDataMapper.selectAllDiffLeve3(sysName, level1, level2));
            return conditionsVO;
        } else if (sysName != null && level1 != null) {
            // 返回第二级条件
            conditionsVO.setLevel2(tableDataMapper.selectAllDiffLeve2(sysName, level1));
            return conditionsVO;
        } else if (sysName != null) {
            // 返回第一级条件
            conditionsVO.setLevel1(tableDataMapper.selectAllDiffLeve1(sysName));
            return conditionsVO;
        }
        return null;
    }

    @Override
    public PageInfo<TableData> getTableData2(String sysName, String level1, String level2, String level3, String level4, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);

        Example example = new Example(TableData.class);
        Example.Criteria criteria = example.createCriteria();
        if (sysName != null) {
            criteria.andEqualTo("system", sysName);
        }
        if (level1 != null) {
            criteria.andEqualTo("jgnmc1", level1);
        }
        if (level2 != null) {
            criteria.andEqualTo("jgnmc2", level2);
        }
        if (level3 != null) {
            criteria.andEqualTo("gnmc3", level3);
        }
        if (level4 != null) {
            criteria.andEqualTo("jgnmc4", level4);
        }
        List<TableData> tableDatas = tableDataMapper.selectByExample(example);

        PageInfo<TableData> tableDataPageInfo = new PageInfo<>(tableDatas);
        return tableDataPageInfo;
    }


}
